package com.yanqu.road.server.manger;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.recharge.RechargeOpenConfig;
import com.yanqu.road.entity.servercenter.IpInfo;
import com.yanqu.road.logic.bussiness.config.RechargeOpenConfigBusiness;
import com.yanqu.road.pb.login.LoginProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.location.IpMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RechargeSwitchConfigMgr extends TempMgr {


    private static Map<String, RechargeOpenConfig> rechargeOpenConfigMap = new ConcurrentHashMap<>();

    @Override
    public boolean reloadConfig() throws Exception {
        rechargeOpenConfigMap = RechargeOpenConfigBusiness.getDataUploadInfo(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()));
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            syncRechargeOpenConfig(player);
        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }


    public static void syncRechargeOpenConfig(GamePlayer player) {
        if (eChannelType.isXiaoChengxuChannel(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()))) {
            RechargeOpenConfig config = getRechargeOpenConfig(player);
            if (config == null) {
                return;
            }
            LoginProto.SyncRechargeOpenConfigMsg.Builder msg = LoginProto.SyncRechargeOpenConfigMsg.newBuilder();
            msg.setIsOpen(config.isOpen());
            msg.setLevel(config.getLevel());
            msg.addAllTimePeriod(config.getTimePeriodList());
            player.sendPacket(ClientProtocol.U_SYNC_RECHARGE_OPEN_CONFIG, msg);
            getLogger().info("userId: {}, config: {}", player.getUserId(), JSON.toJSONString(config));
        }
    }

    private static RechargeOpenConfig getRechargeOpenConfig(GamePlayer player) {
        RechargeOpenConfig config = null;
        String ip = player.getIp();
        getLogger().info("userId: {}, ip: {}", player.getUserId(), ip);
        IpInfo ipInfo = IpMgr.getIpInfo(ip);
        if (ipInfo != null) {
            String city = ipInfo.getCity();
            city = city.replace("市", "");
            config = rechargeOpenConfigMap.get(city);
        }
        if (config == null) {
            config = rechargeOpenConfigMap.get("默认");
        }
        return config;
    }

    public static boolean canPreCharge(GamePlayer player) {
        if (eChannelType.isXiaoChengxuChannel(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()))) {
            if (player.getPhonePlatform() == 2) {
                RechargeOpenConfig config = getRechargeOpenConfig(player);
                if (config != null) {
                    if (!config.isOpen()) {
                        return true;
                    }
                    int level = config.getLevel();
                    int titleId = player.getUserInfo().getTitleId();
                    if (titleId < level) {
                        return false;
                    }
                    List<Integer> timePeriodList = config.getTimePeriodList();
                    int hours = DateHelper.getHours();
                    for (int i = 0; i < timePeriodList.size(); i += 2) {
                        Integer min = timePeriodList.get(i);
                        Integer max = timePeriodList.get(i + 1);
                        if (hours >= min && hours < max) {
                            return true;
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }
}
